﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Kokomo.Mirror.CliMetadata
{
	class BlobRefPropertyDescriptor : MetadataRowPropertyDescriptor
	{
		public BlobRefPropertyDescriptor(
			PropertyDescriptor baseDescriptor,
			MetadataContainer container
			)
			: base(baseDescriptor, container)
		{
		}

		public override object GetValue(object component)
		{
			ICustomTypeDescriptor descriptor = component as ICustomTypeDescriptor;
			BlobRef blobRef = (BlobRef)this.BaseDescriptor.GetValue(descriptor.GetPropertyOwner(this));
			byte[] data = this.Container.GetBlob(blobRef);
			if (data == null) return "<null>";

			StringBuilder builder = new StringBuilder(data.Length * 2);
			for (int i = 0; i < data.Length; i++)
			{
				byte b = data[i];
				builder.Append(b.ToString("X2"));
			}

			string str = builder.ToString();
			return str;
		}

		public override Type PropertyType
		{
			get { return typeof(string); }
		}
	}
}
